package com.jingyanzi.member.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import com.jingyanzi.member.domain.AccountDO;
import com.jingyanzi.member.domain.AccountFinanceDO;
import com.jingyanzi.member.domain.AccountPayDO;
import com.jingyanzi.member.domain.result.ResultDO;
import com.jingyanzi.member.domain.result.ResultSupport;

import com.jingyanzi.member.query.AccountFinanceQueryDO;
import com.jingyanzi.member.query.AccountPayQueryDO;
import com.jingyanzi.member.query.AccountQueryDO;
import com.jingyanzi.member.request.SingleRequest;
import com.jingyanzi.member.response.PageResponse;
import com.jingyanzi.member.response.SingleResponse;
import com.jingyanzi.member.service.IAccountService;

@RestController
@RequestMapping("/account")
public class AccountController {
	@Autowired
	private IAccountService accountService;
	

	@RequestMapping(value = "/detail.json", method = RequestMethod.POST)
	public SingleResponse<AccountDO> detail(SingleRequest<AccountDO> request) {

		AccountDO accountDO = request.getData();

		SingleResponse<AccountDO> response = new SingleResponse<AccountDO>();
		ResultDO result = accountService.get(accountDO.getId());
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(0);
			response.setMsg("");
			response.setData((AccountDO) result.getModel(ResultSupport.FIRST_MODEL_KEY));
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("");
		}

		return response;
	}

	
	/** 查询账户信息列表 */
	@RequestMapping(value = "/queryAccount.json", method = RequestMethod.POST)
	public PageResponse<AccountDO> queryAccount(SingleRequest<AccountQueryDO> request) {

		AccountQueryDO accountQueryDO = request.getData();
		accountQueryDO.setPage(true);
		PageResponse<AccountDO> response = new PageResponse<AccountDO>();

		ResultDO result = accountService.queryAccount(accountQueryDO);
		if (result.isSuccess()) {
			response.setPageindex(accountQueryDO.getPageNO());
			response.setPagesize(accountQueryDO.getPageRows());
			response.setTotal((Long) result.getModel(ResultSupport.SECOND_MODEL_KEY));
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("");
			response.setData((List<AccountDO>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("");
		}

		return response;
	}

	/** 获取客户账户详情 */
	@RequestMapping(value = "/getAccountInfo.json", method = RequestMethod.POST)
	public SingleResponse<AccountDO> GetAccountInfo(SingleRequest<AccountDO> request) throws IOException {
		
		AccountDO accountDO = request.getData();

		ResultDO result = accountService.getAccountDetail(accountDO.getId());
		SingleResponse<AccountDO> response = new SingleResponse<AccountDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("");
			AccountDO account = (AccountDO) result.getModel(ResultSupport.FIRST_MODEL_KEY);
			response.setData(account);
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("");
		}

		return response;
	}

	/** 查询账户信息 */
	@RequestMapping(value = "/getAccount.json", method = RequestMethod.POST)
	public SingleResponse<AccountDO> getAccount(SingleRequest<AccountDO> request) {

		AccountDO accountDO = request.getData();
		SingleResponse<AccountDO> response = new SingleResponse<AccountDO>();

		ResultDO result = accountService.get(accountDO.getId());
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("");
			response.setData((AccountDO) result.getModel(ResultSupport.FIRST_MODEL_KEY));
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("");
		}

		return response;
	}

	// 查询账户资金记录
	@RequestMapping(value = "/queryFinance.json", method = RequestMethod.POST)
	public PageResponse<AccountFinanceDO> queryFinance(SingleRequest<AccountFinanceQueryDO> request) {

		
		AccountFinanceQueryDO queryDO = request.getData();
		queryDO.setPage(true);
		PageResponse<AccountFinanceDO> response = new PageResponse<AccountFinanceDO>();

		ResultDO result = accountService.queryFinance(queryDO);
		if (result.isSuccess()) {
			response.setPageindex(queryDO.getPageNO());
			response.setPagesize(queryDO.getPageRows());
			response.setTotal((Long) result.getModel(ResultSupport.SECOND_MODEL_KEY));
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("");
			response.setData((List<AccountFinanceDO>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("");
		}

		return response;
	}

	// 查询消费记录
	@RequestMapping(value = "/queryPay.json", method = RequestMethod.POST)
	public PageResponse<AccountPayDO> queryPay(SingleRequest<AccountPayQueryDO> request) {
		
		AccountPayQueryDO queryDO = request.getData();
		queryDO.setPage(true);
		PageResponse<AccountPayDO> response = new PageResponse<AccountPayDO>();

		ResultDO result = accountService.queryPay(queryDO);
		if (result.isSuccess()) {
			response.setPageindex(queryDO.getPageNO());
			response.setPagesize(queryDO.getPageRows());
			response.setTotal((Long) result.getModel(ResultSupport.SECOND_MODEL_KEY));
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("");
			response.setData((List<AccountPayDO>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("");
		}

		return response;
	}

	
}